/**
 * @author liubaohui
 * @desc 时间格式化
 * @param {string|number} time  时间字符串或秒时间戳，如 '2018/11/11 12:12:12'
 * @param {string} formatter 需要的格式 默认为 'YYYY-MM-DD hh:mm'
 * @returns {string}
 */
export function timeFormat(time, formatter = 'YYYY-MM-DD hh:mm') {
  if (!time) return ''
  let date = new Date(time * 1000)
  let year, month, day, hour, minute, second

  year = date.getFullYear()
  month = date.getMonth() + 1
  day = date.getDate()
  hour = date.getHours()
  minute = date.getMinutes()
  second = date.getSeconds()

  return formatter
    .replace('YYYY', year)
    .replace('MM', String(month)[1] ? month : `0${month}`)
    .replace('DD', String(day)[1] ? day : `0${day}`)
    .replace('hh', String(hour)[1] ? hour : `0${hour}`)
    .replace('mm', String(minute)[1] ? minute : `0${minute}`)
    .replace('ss', String(second)[1] ? second : `0${second}`)
}
